package org.jeecg.modules.wmhb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.constant.UserConstant;
import org.jeecg.modules.wmhb.entity.*;
import org.jeecg.modules.wmhb.mapper.WmPurchaseOrderDetailMapper;
import org.jeecg.modules.wmhb.service.IWmPurchaseOrderDetailService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Date;
import java.util.List;

/**
 * @Description: 采购订货
 * @Author: jeecg-boot
 * @Date:   2024-11-01
 * @Version: V1.0
 */
@Service
public class WmPurchaseOrderDetailServiceImpl extends ServiceImpl<WmPurchaseOrderDetailMapper, WmPurchaseOrderDetail> implements IWmPurchaseOrderDetailService {
    @Override
    public void arriveBatch(List<String> idList, String factArriveTime){
        LambdaUpdateWrapper<WmPurchaseOrderDetail> updateWrapper = new LambdaUpdateWrapper<>();
        if(StringUtils.isEmpty(factArriveTime)){
            updateWrapper.set(WmPurchaseOrderDetail::getFactArriveTime, new Date());
        }
        else{
            updateWrapper.set(WmPurchaseOrderDetail::getFactArriveTime, factArriveTime);
        }

        updateWrapper.in(WmPurchaseOrderDetail::getId, idList);
        update(null, updateWrapper);
    };

    @Override
    public List<WmPurchaseOrderDetail> searchBySeriesNo(String seriesNo){
        LambdaQueryWrapper<WmPurchaseOrderDetail> queryWrapperTask = new LambdaQueryWrapper<>();
        queryWrapperTask.eq(WmPurchaseOrderDetail::getSeriesNo, seriesNo);
        List<WmPurchaseOrderDetail> wmPurchaseOrderDetailList = list(queryWrapperTask);
        return wmPurchaseOrderDetailList;
    };

    @Override
    public void updateBySeriesNoAndRemark(WmPurchaseOrderDetail wmPurchaseOrderDetail){
        LambdaUpdateWrapper<WmPurchaseOrderDetail> updateWrapper = new LambdaUpdateWrapper<>();
        if(!StringUtils.isEmpty(wmPurchaseOrderDetail.getQuantity())){
            updateWrapper.set(WmPurchaseOrderDetail::getQuantity, wmPurchaseOrderDetail.getQuantity());
        }
        if(!StringUtils.isEmpty(wmPurchaseOrderDetail.getPurchaseOrderNo())){
            updateWrapper.set(WmPurchaseOrderDetail::getPurchaseOrderNo, wmPurchaseOrderDetail.getPurchaseOrderNo());
        }
        if(!StringUtils.isEmpty(wmPurchaseOrderDetail.getJdOrderNo())){
            updateWrapper.set(WmPurchaseOrderDetail::getJdOrderNo, wmPurchaseOrderDetail.getJdOrderNo());
        }
        if(!StringUtils.isEmpty(wmPurchaseOrderDetail.getContractNo())){
            updateWrapper.set(WmPurchaseOrderDetail::getContractNo, wmPurchaseOrderDetail.getContractNo());
        }
        if(!StringUtils.isEmpty(wmPurchaseOrderDetail.getOrderDate())){
            updateWrapper.set(WmPurchaseOrderDetail::getOrderDate, wmPurchaseOrderDetail.getOrderDate());
        }
        if(!StringUtils.isEmpty(wmPurchaseOrderDetail.getPlanArriveDate())){
            updateWrapper.set(WmPurchaseOrderDetail::getPlanArriveDate, wmPurchaseOrderDetail.getPlanArriveDate());
        }
        if(!StringUtils.isEmpty(wmPurchaseOrderDetail.getFactArriveTime())){
            updateWrapper.set(WmPurchaseOrderDetail::getFactArriveTime, wmPurchaseOrderDetail.getFactArriveTime());
        }
        if(!StringUtils.isEmpty(wmPurchaseOrderDetail.getSupplyPeriod())){
            updateWrapper.set(WmPurchaseOrderDetail::getSupplyPeriod, wmPurchaseOrderDetail.getSupplyPeriod());
        }
        if(!StringUtils.isEmpty(wmPurchaseOrderDetail.getJdNo())){
            updateWrapper.set(WmPurchaseOrderDetail::getJdNo, wmPurchaseOrderDetail.getJdNo());
        }
        if(!StringUtils.isEmpty(wmPurchaseOrderDetail.getTaskNo())){
            updateWrapper.set(WmPurchaseOrderDetail::getTaskNo, wmPurchaseOrderDetail.getTaskNo());
        }
        if(!StringUtils.isEmpty(wmPurchaseOrderDetail.getParam())){
            updateWrapper.set(WmPurchaseOrderDetail::getParam, wmPurchaseOrderDetail.getParam());
        }
        if(!StringUtils.isEmpty(wmPurchaseOrderDetail.getCraftRequirement())){
            updateWrapper.set(WmPurchaseOrderDetail::getCraftRequirement, wmPurchaseOrderDetail.getCraftRequirement());
        }
        if(!StringUtils.isEmpty(wmPurchaseOrderDetail.getReceiver())){
            updateWrapper.set(WmPurchaseOrderDetail::getReceiver, wmPurchaseOrderDetail.getReceiver());
        }
        if(!StringUtils.isEmpty(wmPurchaseOrderDetail.getIsPurchase())){
            updateWrapper.set(WmPurchaseOrderDetail::getIsPurchase, wmPurchaseOrderDetail.getIsPurchase());
        }
        if(!StringUtils.isEmpty(wmPurchaseOrderDetail.getRequireArriveTime())){
            updateWrapper.set(WmPurchaseOrderDetail::getRequireArriveTime, wmPurchaseOrderDetail.getRequireArriveTime());
        }
        if(!StringUtils.isEmpty(wmPurchaseOrderDetail.getPurchaseApplyTime())){
            updateWrapper.set(WmPurchaseOrderDetail::getPurchaseApplyTime, wmPurchaseOrderDetail.getPurchaseApplyTime());
        }
        if(!StringUtils.isEmpty(wmPurchaseOrderDetail.getRemark())){
            updateWrapper.set(WmPurchaseOrderDetail::getRemark, wmPurchaseOrderDetail.getRemark());
        }
        if(!StringUtils.isEmpty(wmPurchaseOrderDetail.getVendorAPrice())){
            updateWrapper.set(WmPurchaseOrderDetail::getVendorAPrice, wmPurchaseOrderDetail.getVendorAPrice());
        }
        if(!StringUtils.isEmpty(wmPurchaseOrderDetail.getVendorBPrice())){
            updateWrapper.set(WmPurchaseOrderDetail::getVendorBPrice, wmPurchaseOrderDetail.getVendorBPrice());
        }
        if(!StringUtils.isEmpty(wmPurchaseOrderDetail.getVendorCPrice())){
            updateWrapper.set(WmPurchaseOrderDetail::getVendorCPrice, wmPurchaseOrderDetail.getVendorCPrice());
        }
        if(!StringUtils.isEmpty(wmPurchaseOrderDetail.getVendorDPrice())){
            updateWrapper.set(WmPurchaseOrderDetail::getVendorDPrice, wmPurchaseOrderDetail.getVendorDPrice());
        }
        if(!StringUtils.isEmpty(wmPurchaseOrderDetail.getVendorEPrice())){
            updateWrapper.set(WmPurchaseOrderDetail::getVendorEPrice, wmPurchaseOrderDetail.getVendorEPrice());
        }
        if(!StringUtils.isEmpty(wmPurchaseOrderDetail.getVendorFPrice())){
            updateWrapper.set(WmPurchaseOrderDetail::getVendorFPrice, wmPurchaseOrderDetail.getVendorFPrice());
        }
        updateWrapper.eq(WmPurchaseOrderDetail::getSeriesNo, wmPurchaseOrderDetail.getSeriesNo());
        update(null, updateWrapper);
    };

    @Override
    public List<WmPurchaseOrderDetail> searchByContractAndJdName(String contractNo, String jdName){
        LambdaQueryWrapper<WmPurchaseOrderDetail> queryWrapperTask = new LambdaQueryWrapper<>();
        queryWrapperTask.eq(WmPurchaseOrderDetail::getContractNo, contractNo).eq(WmPurchaseOrderDetail::getJdName, jdName);
        List<WmPurchaseOrderDetail> wmPurchaseOrderDetailList = list(queryWrapperTask);
        return wmPurchaseOrderDetailList;
    };

    @Override
    public void updateByContractAndJdName(WmPurchaseOrderDetail wmPurchaseOrderDetail){
        LambdaUpdateWrapper<WmPurchaseOrderDetail> updateWrapper = new LambdaUpdateWrapper<>();
        if(!StringUtils.isEmpty(wmPurchaseOrderDetail.getQuantity())){
            updateWrapper.set(WmPurchaseOrderDetail::getQuantity, wmPurchaseOrderDetail.getQuantity());
        }
        if(!StringUtils.isEmpty(wmPurchaseOrderDetail.getPurchaseOrderNo())){
            updateWrapper.set(WmPurchaseOrderDetail::getPurchaseOrderNo, wmPurchaseOrderDetail.getPurchaseOrderNo());
        }
        if(!StringUtils.isEmpty(wmPurchaseOrderDetail.getJdOrderNo())){
            updateWrapper.set(WmPurchaseOrderDetail::getJdOrderNo, wmPurchaseOrderDetail.getJdOrderNo());
        }
        if(!StringUtils.isEmpty(wmPurchaseOrderDetail.getContractNo())){
            updateWrapper.set(WmPurchaseOrderDetail::getContractNo, wmPurchaseOrderDetail.getContractNo());
        }
        if(!StringUtils.isEmpty(wmPurchaseOrderDetail.getOrderDate())){
            updateWrapper.set(WmPurchaseOrderDetail::getOrderDate, wmPurchaseOrderDetail.getOrderDate());
        }
        if(!StringUtils.isEmpty(wmPurchaseOrderDetail.getPlanArriveDate())){
            updateWrapper.set(WmPurchaseOrderDetail::getPlanArriveDate, wmPurchaseOrderDetail.getPlanArriveDate());
        }
        if(!StringUtils.isEmpty(wmPurchaseOrderDetail.getFactArriveTime())){
            updateWrapper.set(WmPurchaseOrderDetail::getFactArriveTime, wmPurchaseOrderDetail.getFactArriveTime());
        }
        if(!StringUtils.isEmpty(wmPurchaseOrderDetail.getSupplyPeriod())){
            updateWrapper.set(WmPurchaseOrderDetail::getSupplyPeriod, wmPurchaseOrderDetail.getSupplyPeriod());
        }
        if(!StringUtils.isEmpty(wmPurchaseOrderDetail.getJdNo())){
            updateWrapper.set(WmPurchaseOrderDetail::getJdNo, wmPurchaseOrderDetail.getJdNo());
        }
        if(!StringUtils.isEmpty(wmPurchaseOrderDetail.getTaskNo())){
            updateWrapper.set(WmPurchaseOrderDetail::getTaskNo, wmPurchaseOrderDetail.getTaskNo());
        }
        if(!StringUtils.isEmpty(wmPurchaseOrderDetail.getParam())){
            updateWrapper.set(WmPurchaseOrderDetail::getParam, wmPurchaseOrderDetail.getParam());
        }
        if(!StringUtils.isEmpty(wmPurchaseOrderDetail.getCraftRequirement())){
            updateWrapper.set(WmPurchaseOrderDetail::getCraftRequirement, wmPurchaseOrderDetail.getCraftRequirement());
        }
        if(!StringUtils.isEmpty(wmPurchaseOrderDetail.getReceiver())){
            updateWrapper.set(WmPurchaseOrderDetail::getReceiver, wmPurchaseOrderDetail.getReceiver());
        }
        if(!StringUtils.isEmpty(wmPurchaseOrderDetail.getIsPurchase())){
            updateWrapper.set(WmPurchaseOrderDetail::getIsPurchase, wmPurchaseOrderDetail.getIsPurchase());
        }
        if(!StringUtils.isEmpty(wmPurchaseOrderDetail.getRequireArriveTime())){
            updateWrapper.set(WmPurchaseOrderDetail::getRequireArriveTime, wmPurchaseOrderDetail.getRequireArriveTime());
        }
        if(!StringUtils.isEmpty(wmPurchaseOrderDetail.getPurchaseApplyTime())){
            updateWrapper.set(WmPurchaseOrderDetail::getPurchaseApplyTime, wmPurchaseOrderDetail.getPurchaseApplyTime());
        }
        if(!StringUtils.isEmpty(wmPurchaseOrderDetail.getRemark())){
            updateWrapper.set(WmPurchaseOrderDetail::getRemark, wmPurchaseOrderDetail.getRemark());
        }
        if(!StringUtils.isEmpty(wmPurchaseOrderDetail.getVendorAPrice())){
            updateWrapper.set(WmPurchaseOrderDetail::getVendorAPrice, wmPurchaseOrderDetail.getVendorAPrice());
        }
        if(!StringUtils.isEmpty(wmPurchaseOrderDetail.getVendorBPrice())){
            updateWrapper.set(WmPurchaseOrderDetail::getVendorBPrice, wmPurchaseOrderDetail.getVendorBPrice());
        }
        if(!StringUtils.isEmpty(wmPurchaseOrderDetail.getVendorCPrice())){
            updateWrapper.set(WmPurchaseOrderDetail::getVendorCPrice, wmPurchaseOrderDetail.getVendorCPrice());
        }
        if(!StringUtils.isEmpty(wmPurchaseOrderDetail.getVendorDPrice())){
            updateWrapper.set(WmPurchaseOrderDetail::getVendorDPrice, wmPurchaseOrderDetail.getVendorDPrice());
        }
        if(!StringUtils.isEmpty(wmPurchaseOrderDetail.getVendorEPrice())){
            updateWrapper.set(WmPurchaseOrderDetail::getVendorEPrice, wmPurchaseOrderDetail.getVendorEPrice());
        }
        if(!StringUtils.isEmpty(wmPurchaseOrderDetail.getVendorFPrice())){
            updateWrapper.set(WmPurchaseOrderDetail::getVendorFPrice, wmPurchaseOrderDetail.getVendorFPrice());
        }
        updateWrapper.eq(WmPurchaseOrderDetail::getContractNo, wmPurchaseOrderDetail.getContractNo()).eq(WmPurchaseOrderDetail::getJdName, wmPurchaseOrderDetail.getJdName());
        update(null, updateWrapper);
    };

    @Override
    public List<WmPurchaseOrderDetail> searchByJdOrderAndJdName(String jdOrderNo, String jdName){
        LambdaQueryWrapper<WmPurchaseOrderDetail> queryWrapperTask = new LambdaQueryWrapper<>();
        queryWrapperTask.eq(WmPurchaseOrderDetail::getJdOrderNo, jdOrderNo).eq(WmPurchaseOrderDetail::getJdName, jdName);
        List<WmPurchaseOrderDetail> wmPurchaseOrderDetailList = list(queryWrapperTask);
        return wmPurchaseOrderDetailList;
    };

    @Override
    public void updateByJdOrderAndJdName(WmPurchaseOrderDetail wmPurchaseOrderDetail){
        LambdaUpdateWrapper<WmPurchaseOrderDetail> updateWrapper = new LambdaUpdateWrapper<>();
        if(!StringUtils.isEmpty(wmPurchaseOrderDetail.getQuantity())){
            updateWrapper.set(WmPurchaseOrderDetail::getQuantity, wmPurchaseOrderDetail.getQuantity())
                    .set(WmPurchaseOrderDetail::getArriveQuantity, wmPurchaseOrderDetail.getArriveQuantity());
        }
        if(!StringUtils.isEmpty(wmPurchaseOrderDetail.getPurchaseOrderNo())){
            updateWrapper.set(WmPurchaseOrderDetail::getPurchaseOrderNo, wmPurchaseOrderDetail.getPurchaseOrderNo());
        }
        if(!StringUtils.isEmpty(wmPurchaseOrderDetail.getJdOrderNo())){
            updateWrapper.set(WmPurchaseOrderDetail::getJdOrderNo, wmPurchaseOrderDetail.getJdOrderNo());
        }
        if(!StringUtils.isEmpty(wmPurchaseOrderDetail.getContractNo())){
            updateWrapper.set(WmPurchaseOrderDetail::getContractNo, wmPurchaseOrderDetail.getContractNo());
        }
        if(!StringUtils.isEmpty(wmPurchaseOrderDetail.getOrderDate())){
            updateWrapper.set(WmPurchaseOrderDetail::getOrderDate, wmPurchaseOrderDetail.getOrderDate());
        }
        if(!StringUtils.isEmpty(wmPurchaseOrderDetail.getPlanArriveDate())){
            updateWrapper.set(WmPurchaseOrderDetail::getPlanArriveDate, wmPurchaseOrderDetail.getPlanArriveDate());
        }
        if(!StringUtils.isEmpty(wmPurchaseOrderDetail.getFactArriveTime())){
            updateWrapper.set(WmPurchaseOrderDetail::getFactArriveTime, wmPurchaseOrderDetail.getFactArriveTime());
        }
        if(!StringUtils.isEmpty(wmPurchaseOrderDetail.getSupplyPeriod())){
            updateWrapper.set(WmPurchaseOrderDetail::getSupplyPeriod, wmPurchaseOrderDetail.getSupplyPeriod());
        }
        if(!StringUtils.isEmpty(wmPurchaseOrderDetail.getJdNo())){
            updateWrapper.set(WmPurchaseOrderDetail::getJdNo, wmPurchaseOrderDetail.getJdNo());
        }
        if(!StringUtils.isEmpty(wmPurchaseOrderDetail.getTaskNo())){
            updateWrapper.set(WmPurchaseOrderDetail::getTaskNo, wmPurchaseOrderDetail.getTaskNo());
        }
        if(!StringUtils.isEmpty(wmPurchaseOrderDetail.getParam())){
            updateWrapper.set(WmPurchaseOrderDetail::getParam, wmPurchaseOrderDetail.getParam());
        }
        if(!StringUtils.isEmpty(wmPurchaseOrderDetail.getCraftRequirement())){
            updateWrapper.set(WmPurchaseOrderDetail::getCraftRequirement, wmPurchaseOrderDetail.getCraftRequirement());
        }
        if(!StringUtils.isEmpty(wmPurchaseOrderDetail.getReceiver())){
            updateWrapper.set(WmPurchaseOrderDetail::getReceiver, wmPurchaseOrderDetail.getReceiver());
        }
        if(!StringUtils.isEmpty(wmPurchaseOrderDetail.getIsPurchase())){
            updateWrapper.set(WmPurchaseOrderDetail::getIsPurchase, wmPurchaseOrderDetail.getIsPurchase());
        }
        if(!StringUtils.isEmpty(wmPurchaseOrderDetail.getRequireArriveTime())){
            updateWrapper.set(WmPurchaseOrderDetail::getRequireArriveTime, wmPurchaseOrderDetail.getRequireArriveTime());
        }
        if(!StringUtils.isEmpty(wmPurchaseOrderDetail.getPurchaseApplyTime())){
            updateWrapper.set(WmPurchaseOrderDetail::getPurchaseApplyTime, wmPurchaseOrderDetail.getPurchaseApplyTime());
        }
        if(!StringUtils.isEmpty(wmPurchaseOrderDetail.getRemark())){
            updateWrapper.set(WmPurchaseOrderDetail::getRemark, wmPurchaseOrderDetail.getRemark());
        }
        if(!StringUtils.isEmpty(wmPurchaseOrderDetail.getVendorAPrice())){
            updateWrapper.set(WmPurchaseOrderDetail::getVendorAPrice, wmPurchaseOrderDetail.getVendorAPrice());
        }
        if(!StringUtils.isEmpty(wmPurchaseOrderDetail.getVendorBPrice())){
            updateWrapper.set(WmPurchaseOrderDetail::getVendorBPrice, wmPurchaseOrderDetail.getVendorBPrice());
        }
        if(!StringUtils.isEmpty(wmPurchaseOrderDetail.getVendorCPrice())){
            updateWrapper.set(WmPurchaseOrderDetail::getVendorCPrice, wmPurchaseOrderDetail.getVendorCPrice());
        }
        if(!StringUtils.isEmpty(wmPurchaseOrderDetail.getVendorDPrice())){
            updateWrapper.set(WmPurchaseOrderDetail::getVendorDPrice, wmPurchaseOrderDetail.getVendorDPrice());
        }
        if(!StringUtils.isEmpty(wmPurchaseOrderDetail.getVendorEPrice())){
            updateWrapper.set(WmPurchaseOrderDetail::getVendorEPrice, wmPurchaseOrderDetail.getVendorEPrice());
        }
        if(!StringUtils.isEmpty(wmPurchaseOrderDetail.getVendorFPrice())){
            updateWrapper.set(WmPurchaseOrderDetail::getVendorFPrice, wmPurchaseOrderDetail.getVendorFPrice());
        }
        updateWrapper.eq(WmPurchaseOrderDetail::getJdOrderNo, wmPurchaseOrderDetail.getJdOrderNo()).eq(WmPurchaseOrderDetail::getJdName, wmPurchaseOrderDetail.getJdName());
        update(null, updateWrapper);
    };

    @Override
    public List<WmPurchaseOrderDetail> queryNoSeriesNoOfPurchaseBom(){
        LambdaQueryWrapper<WmPurchaseOrderDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.isNull(WmPurchaseOrderDetail::getSeriesNo).eq(WmPurchaseOrderDetail::getIsCheckPurchaseBom, UserConstant.SYS_NO);
        List<WmPurchaseOrderDetail> wmPurchaseOrderDetailList = list(queryWrapper);
        return wmPurchaseOrderDetailList;
    };

    @Override
    public List<WmPurchaseOrderDetail> queryNoSeriesNoOfTaskBom(WmTaskBom wmTaskBom, WmTaskbook wmTaskbook){
        LambdaQueryWrapper<WmPurchaseOrderDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.isNull(WmPurchaseOrderDetail::getSeriesNo)
                .eq(WmPurchaseOrderDetail::getIsCheckTaskBom, UserConstant.SYS_NO)
                .eq(WmPurchaseOrderDetail::getJdName, wmTaskBom.getName())
                .like(WmPurchaseOrderDetail::getRemark, wmTaskbook.getCode());
        List<WmPurchaseOrderDetail> wmPurchaseOrderDetailList = list(queryWrapper);
        return wmPurchaseOrderDetailList;
    };
}
